package com.iris.live.services.report.sources;

import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.BaseReportDataSource;
import com.iris.live.services.report.data.ReportContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 保险合作机构汇总资源
 * Created by peng on 2017/12/27.
 */
@Component("InsuranceCompanyDataSource")
public class InsuranceCompanyDataSource extends BaseReportDataSource {

    @Override
    public List getData(ReportContext context, List<ReportConfig> config) throws Exception {
        Map info= context.getParams() ;
        List<Map> datas =new ArrayList<>();
        datas =  getOrgLineData(info);
        return  datas;
    }
    //按地域
    public List getOrgLineData(Map info){
        String sql="SELECT\n" +
                "'LSH' 集团,\n" +
                "\tc.大区_EN AS 大区_EN,\n" +
                "\tc.小区_EN AS 小区_EN,\n" +
                "IF (\n" +
                "\tisnull(c.CTA_EN)\n" +
                "\tOR c.CTA_EN = '',\n" +
                "\t'Other CTA',\n" +
                "\tc.CTA_EN\n" +
                ") AS CTA_EN,\n" +
                "\tc.许可_LSH,\n" +
                " b.`保险公司编号`,\n" +
                "\t\tb.`保险公司`,\n" +
                "\t\t'是' `启用状态`,\n" +
                "\t\tCOUNT(*) AS 合作数量\n" +
                "\tFROM\n" +
                "\t\tlsh保险公司使用 a,\n" +
                "\t\tlsh保险公司设置 b,\n" +
                "\t\tlsh经销商表 c\n" +
                "\tWHERE\n" +
                "\t\ta.`保险公司编号` = b.`保险公司编号`\n" +
                "\tAND a.`许可` = c.`许可_BMBS`\n" +
                "\tAND b.`启用状态` = '启用'\n" +
                "\tAND a.`使用状态` = '启用' GROUP BY\n" +
                "\t\tb.`保险公司编号`,\n" +
                "\t\tb.`保险公司`,\n" +
                "\t\tc.`许可_LSH`\n" +
                "\tUNION ALL\n" +
                "SELECT 'LSH' 集团,a.`大区_EN`,a.小区_EN,IF (\n" +
                "\tisnull(a.CTA_EN)\n" +
                "\tOR a.CTA_EN = '',\n" +
                "\t'Other CTA',\n" +
                "\ta.CTA_EN\n" +
                ") AS CTA_EN,a.`许可_LSH`, '' 保险公司编号,'经销商总数' 保险公司,'是' `启用状态`, 1 合作数量\n" +
                "FROM lsh经销商表 a";
        return this.getList(sql);
    }
}
